package com.zjun.core.snowflake;

import com.zjun.toolkit.SnowFlakeIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

/**
 * 雪花算法模板生成
 */
@Slf4j
public abstract class AbstractWorkIdChooseTemplate {
    /**
     * 是否使用当前系统时间戳
     */
    @Value("${framework.distributed.id.snowflake.is-use-system-clock:false}")
    private boolean isUseSystemClock;

    /**
     * 根据自定义策略生成WorkId生成器
     * @return
     */
    protected abstract WorkWrapper chooseWorkId();

    public void chooseAndInit(){
        //模板方法：通过抽象方法获取WorkId包装器创建雪花算法
        WorkWrapper workWrapper = chooseWorkId();
        long workId = workWrapper.getWorkId();
        long dataCenterId = workWrapper.getDataCenterId();
        Snowflake snowflake = new Snowflake(workId,dataCenterId,isUseSystemClock);
        log.info("[AbstractWorkIdChooseTemplate]"+"Snowflake type: {}, workId: {}, dataCenterId: {}", this.getClass().getSimpleName(), workId, dataCenterId);
        SnowFlakeIdUtil.initSnowFlake(snowflake);
    }
}
